package org.fintool.fileio;

import java.io.BufferedReader;
import java.io.File;
import java.util.ArrayList;

import org.fintool.common.Constants;
import org.fintool.common.PropertyUtil;

public class FileReader {

	static PropertyUtil propertyUtil=null;

	static String strBaseFolderPath=null;

	static{
           propertyUtil=new PropertyUtil();
           if(propertyUtil!=null){
        	   strBaseFolderPath=propertyUtil.getValue(Constants.BASE_FILE_PATH);

           }
	}

	public final ArrayList getRawDataAsLines(String filePath){
		//System.out.println(filePath);

		ArrayList returnList=null;
		BufferedReader br=null;
		try{
		br=new BufferedReader(new java.io.FileReader(filePath));
		String strCurrentLine=null;
		returnList=new ArrayList();

		while((strCurrentLine=br.readLine())!=null){
			returnList.add(strCurrentLine);
		}
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			try{
			br.close();
			}catch (Exception e) {
				// TODO: handle exception
			}
		}

		return returnList;

	}

	public final String getFileDirPath(String stock,String exchange){
		String strReturn=null;

		if(propertyUtil.isDay()){
		strReturn=strBaseFolderPath+File.separator+exchange+File.separator+stock+File.separator+Constants.FOLDER_DAY;
		}

		return strReturn;

	}

	public final String getTransFormFileDirPath(String stock,String exchange){
		String strReturn=null;

		if(propertyUtil.isDay()){
		strReturn=strBaseFolderPath+File.separator+exchange+File.separator+stock+File.separator+Constants.FOLDER_TRANSFORM;
		}

		return strReturn;

	}

	public String getTransformFileLastDate(String stock,String exchange) {

		File directory  = null;
		//long lastFileTS=0l;
		String fileName=null;;
		File[] files=null;
		try{

			if(propertyUtil.isDay()){
			directory=new File(getTransFormFileDirPath(stock,exchange));
			if(directory!=null ){
				files=directory.listFiles();
				if(files!=null){
					fileName=files[0].getName();

				}

			}

			}else{

			}



		}catch (Exception e) {
			// TODO: handle exception
		}

		return fileName;

	}


	public long getFileLastDate(String stock,String exchange) {

		File directory  = null;
		long lastFileTS=0l;
		File[] files=null;
		try{

			if(propertyUtil.isDay()){
			directory=new File(getFileDirPath(stock,exchange));
			if(directory!=null ){
				files=directory.listFiles();
				if(files!=null){
					lastFileTS=Long.parseLong(files[0].getName());

				}

			}

			}else{

			}



		}catch (Exception e) {
			// TODO: handle exception
		}

		return lastFileTS;

	}

}
